﻿<Serializable()>
Public Class L3DPlatformPlan
    Private pPresentationName As String
    Private pFontFamily As String = "arial"
    Private pPresentationOwner As String
    Private pPresentationModel As String
    Private pPresentationStatusDate As Date
    Private WithEvents ProjectsValue As New VMGenLibrary.VMObjects.PowerCollection(Of L3DObjectsLibrary.Project)
    Private TabPageChanged As Boolean = True
    Private DragObjectValue As New Object
    Private LastModificationSavedValue As Boolean = True
    Public Event QueryChange(ByVal sender As Object, ByVal e As QueryChangeEventArgs)

#Region "PROPERTIES"
    Public ReadOnly Property DragObject As Object
        Get
            Return DragObjectValue
        End Get
    End Property
    Private ReadOnly Property ActiveProjectPage As Windows.Forms.TabPage
        Get
            Return tabProjectContainer.SelectedTab
        End Get
    End Property
    Public Property PresentationOwner As String
        Get
            Return pPresentationOwner
        End Get
        Set(ByVal value As String)
            pPresentationOwner = value
            txtOwner.Text = value
        End Set
    End Property
    Public Property PresentationModel As String
        Get
            Return pPresentationModel
        End Get
        Set(ByVal value As String)
            pPresentationModel = value
            txtModello.Text = value
        End Set
    End Property
    Public ReadOnly Property ActiveProject As L3DObjectsLibrary.ProjectControl
        Get
            Dim Result As L3DObjectsLibrary.ProjectControl = Nothing
            If tabProjectContainer.SelectedTab IsNot Nothing Then
                Dim PrList As List(Of Windows.Forms.UserControl) = (From Ctr As Windows.Forms.UserControl In tabProjectContainer.SelectedTab.Controls Where Ctr.GetType = GetType(ProjectControl)).ToList
                If PrList.Count > 0 Then
                    Result = PrList.First
                End If
            End If
            Return Result
        End Get
    End Property
    Public Property Projects As VMGenLibrary.VMObjects.PowerCollection(Of L3DObjectsLibrary.Project)
        Get
            Return ProjectsValue
        End Get
        Set(ByVal value As VMGenLibrary.VMObjects.PowerCollection(Of L3DObjectsLibrary.Project))
            ProjectsValue.Dispose()
            ProjectsValue = value
        End Set
    End Property
    Public Property FontFamily As String
        Get
            Return pFontFamily
        End Get
        Set(ByVal value As String)
            pFontFamily = value
        End Set
    End Property
    Public Property PresentationName As String
        Get
            Return pPresentationName
        End Get
        Set(ByVal value As String)
            pPresentationName = value
        End Set
    End Property
    Public Property PresentationStatusDate As Date
        Get
            If DateTime.Compare(pPresentationStatusDate, #1/1/2010#) < 0 Then
                pPresentationStatusDate = Now
            End If
            Return pPresentationStatusDate
        End Get
        Set(ByVal value As Date)
            pPresentationStatusDate = value
        End Set
    End Property
    Public Property LastModificationSaved As Boolean
        Get
            Return LastModificationSavedValue
        End Get
        Set(ByVal value As Boolean)
            LastModificationSavedValue = value
        End Set
    End Property
#End Region
#Region "METHODS"
    Public Sub Clear()
        tabProjectContainer.TabPages.Clear()
        Projects.Clear()
        PresentationOwner = "Anonymous"
        PresentationModel = "Unknown"
    End Sub
    Public Sub SetAllModelsOwners()
        For Each Pr As Project In Projects
            With Pr
                .Model = PresentationModel
                .L3Owner = PresentationOwner
                .ApplicationOwner = PresentationOwner
            End With
        Next
    End Sub
    Private Sub RepositionEditor(ByVal Page As Windows.Forms.TabPage)
        ActiveProject.Editor.Left = 15
        ActiveProject.Editor.Top = 15
    End Sub
    Private Sub UpdateProjectData()
        TSCZoom.Text = ActiveProject.ZoomFactor * 100
    End Sub
    Friend Function NewProjectPage(Optional ByVal Project As L3DObjectsLibrary.Project = Nothing) As Windows.Forms.TabPage
        Dim NewPage As New Windows.Forms.TabPage
        If Project Is Nothing Then
            Project = New L3DObjectsLibrary.Project
        End If
        Dim ProjectCtr As New ProjectControl
        With ProjectCtr
            .Project = Project
            .Dock = Windows.Forms.DockStyle.Fill
            AddHandler .Project.PropertyChange, AddressOf Project_PropertyChange
            AddHandler .UndoRedoPerformed, AddressOf ProjectControl_UndoRedoPerformed
            AddHandler .QueryChange, AddressOf ProjectControl_QueryChange
        End With
        With NewPage
            .Controls.Add(ProjectCtr)
            .Text = Project.Name
        End With
        Return NewPage
    End Function
    Public Function GenerateNewProjectElement() As L3DObjectsLibrary.ProjectElement
        Dim El As New L3DObjectsLibrary.ProjectElement
        El.Groups.Add(GenerateNewGroup)
        El.Name = "Fase"
        Return El
    End Function
    Public Function GenerateNewCar() As L3DObjectsLibrary.Car
        Dim Car As New L3DObjectsLibrary.Car
        With Car
            .Name = "Nuova"
        End With
        Return Car
    End Function
    Public Function GenerateNewMilestone() As L3DObjectsLibrary.ProjectElement
        Dim El As New L3DObjectsLibrary.ProjectElement
        Dim Milestone As New L3DObjectsLibrary.Milestone
        With Milestone
            .MilestoneDate = Now
            .MilestoneRealDate = Now
        End With
        El.Milestones.Add(Milestone)
        Return El
    End Function
    Public Function GenerateNewGroup() As L3DObjectsLibrary.Group
        Dim Group As New L3DObjectsLibrary.Group
        Group.Cars.Add(GenerateNewCar)
        Return Group
    End Function
#End Region
#Region "EVENTS"
    Private Sub ProjectControl_QueryChange(ByVal sender As Object, ByVal e As QueryChangeEventArgs)
        LastModificationSaved = False
    End Sub
    Private Sub ProjectControl_UndoRedoPerformed(ByVal sender As Object, ByVal UndoRedo As UndoRedoValue)
        If sender.GetType = GetType(ProjectControl) Then
            tabProjectContainer.SelectedTab.Text = CType(sender, ProjectControl).Project.Name
        End If
    End Sub
    Private Sub Project_PropertyChange(ByVal sender As Object)
        If sender.GetType = GetType(Project) Then
            tabProjectContainer.SelectedTab.Text = CType(sender, Project).Name
        End If
    End Sub
    Private Sub Projects_CountChanged(ByVal sender As VMGenLibrary.VMObjects.PowerCollection(Of L3DObjectsLibrary.Project), ByVal e As VMGenLibrary.Structures.PowercollectionEventArgs) Handles ProjectsValue.CountChanged
        Dim Project As Project = CType(e.ObjectMoved, Project)
        If e.Action = VMGenLibrary.Structures.CountChangedAction.Added Then
            Me.tabProjectContainer.TabPages.Add(NewProjectPage(Project))
        End If
        UpdateProjectData()
    End Sub
    Private Sub tabProjectContainer_resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles tabProjectContainer.Resize
        Dim TargetTabControl As Windows.Forms.TabControl = sender
        If TargetTabControl.TabPages.Count > 0 Then
            If TargetTabControl.SelectedTab.Controls.Count > 0 Then
                RepositionEditor(TargetTabControl.SelectedTab)
            End If
        End If
    End Sub
    Private Sub tabProjectContainer_Click(ByVal sender As Object, ByVal e As Windows.Forms.MouseEventArgs) Handles tabProjectContainer.Click
        If e.Button = Windows.Forms.MouseButtons.Left Then
            If TabPageChanged = True Then
                TabPageChanged = False
                Exit Sub
            ElseIf (e.Clicks = 1) Then
                mnuModificaProgetto.PerformClick()
            End If
        End If
    End Sub
    Private Sub mnuModifica_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuModificaProgetto.Click
        ActiveProject.EditMode = Not (ActiveProject.EditMode)
        RepositionEditor(tabProjectContainer.SelectedTab)
    End Sub
    Private Sub mnuElimina_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuEliminaProgetto.Click
        Dim Conferma = MsgBox("Eliminare progetto " & tabProjectContainer.SelectedTab.Text & "?", MsgBoxStyle.YesNo, "Eliminazione progetto")
        If Conferma = vbYes Then
            Dim Index As Integer = Projects.IndexOf(ActiveProject.Project)
            Projects.RemoveAt(Index)
            tabProjectContainer.TabPages.RemoveAt(tabProjectContainer.SelectedIndex)
        End If
    End Sub
    Private Sub tabProjectContainer_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tabProjectContainer.DoubleClick, mnuNuovoProgetto.Click
        ActiveProject.EditMode = False
        Dim Project As New L3DObjectsLibrary.Project
        Projects.Add(Project)
        tabProjectContainer.SelectedTab = tabProjectContainer.TabPages(tabProjectContainer.TabPages.Count - 1)
        LastModificationSaved = False
    End Sub
    Private Sub tabProjectContainer_TabIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles tabProjectContainer.SelectedIndexChanged
        TabPageChanged = True
        If ActiveProject IsNot Nothing Then
            TSCZoom.Text = ActiveProject.ZoomFactor * 100
        End If
    End Sub
    Private Sub L3DPlatformPlan_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Projects.Add(New L3DObjectsLibrary.Project)
    End Sub
    Private Sub txtModello_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If sender Is txtModello Then
            PresentationModel = txtModello.Text
        ElseIf sender Is txtOwner Then
            PresentationOwner = txtOwner.Text
        End If
    End Sub
    Private Sub ObjectAdditionRequest(ByVal ObjectToAdd As Object)
        If ObjectToAdd Is Nothing Then
            Exit Sub
        End If
        If ObjectToAdd.GetType = GetType(L3DObjectsLibrary.Milestone) Then
            ActiveProject.MakeUndoPoint(UndoType.ElAdd)
            Dim El As New L3DObjectsLibrary.ProjectElement
            El.Milestones.Add(ObjectToAdd)
            ActiveProject.Project.ProjectElements.Add(El)
        ElseIf ObjectToAdd.GetType = GetType(L3DObjectsLibrary.ProjectElement) Then
            ActiveProject.MakeUndoPoint(UndoType.ElAdd)
            ActiveProject.Project.ProjectElements.Add(ObjectToAdd)
        ElseIf ObjectToAdd.GetType = GetType(L3DObjectsLibrary.Group) Then
            If ActiveProject.Project.ProjectElements.LastAdded IsNot Nothing Then
                If (ActiveProject.Project.ProjectElements.LastAdded.ElementType = ElementTypeValue.L3DPhase Or ActiveProject.Project.ProjectElements.LastAdded.ElementType = ElementTypeValue.L3DMixed) Then
                    ActiveProject.MakeUndoPoint(UndoType.GroupAdd)
                    ActiveProject.Project.ProjectElements.LastAdded.Groups.Add(CType(ObjectToAdd, L3DObjectsLibrary.Group))
                Else
                    ActiveProject.MakeUndoPoint(UndoType.ElAdd)
                    Dim Element As L3DObjectsLibrary.ProjectElement = GenerateNewProjectElement()
                    ActiveProject.Project.ProjectElements.Add(Element)
                End If
            Else
                ActiveProject.MakeUndoPoint(UndoType.ElAdd)
                Dim Element As L3DObjectsLibrary.ProjectElement = GenerateNewProjectElement()
                ActiveProject.Project.ProjectElements.Add(Element)
            End If
        ElseIf ObjectToAdd.GetType = GetType(L3DObjectsLibrary.Car) Then
            If ActiveProject.Project.ProjectElements.LastAdded IsNot Nothing Then
                If (ActiveProject.Project.ProjectElements.LastAdded.ElementType = ElementTypeValue.L3DPhase Or ActiveProject.Project.ProjectElements.LastAdded.ElementType = ElementTypeValue.L3DMixed) Then
                    If ActiveProject.Project.ProjectElements.LastAdded.Groups.LastAdded IsNot Nothing Then
                        ActiveProject.MakeUndoPoint(UndoType.ElAdd)
                        ActiveProject.Project.ProjectElements.LastAdded.Groups.LastAdded.Cars.Add(CType(ObjectToAdd, L3DObjectsLibrary.Car))
                    End If
                Else
                    ActiveProject.MakeUndoPoint(UndoType.ElAdd)
                    Dim Element As L3DObjectsLibrary.ProjectElement = GenerateNewProjectElement()
                    ActiveProject.Project.ProjectElements.Add(Element)
                End If
            Else
                ActiveProject.MakeUndoPoint(UndoType.ElAdd)
                Dim Element As L3DObjectsLibrary.ProjectElement = GenerateNewProjectElement()
                ActiveProject.Project.ProjectElements.Add(Element)
            End If
        End If
    End Sub
    Private Sub TSCZoom_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSCZoom.SelectedIndexChanged
        Dim ZoomValue As Single = Val(TSCZoom.Text) / 100
        ActiveProject.ZoomFactor = ZoomValue
    End Sub
    Private Sub TSElements_MouseDown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBMilestone.MouseDown, TSBPhase.MouseDown, TSBGroup.MouseDown, TSBCar.MouseDown
        If sender Is TSBMilestone Then
            DragObjectValue = GenerateNewMilestone()
        ElseIf sender Is TSBPhase Then
            DragObjectValue = GenerateNewProjectElement()
        ElseIf sender Is TSBGroup Then
            DragObjectValue = GenerateNewGroup()
        ElseIf sender Is TSBCar Then
            DragObjectValue = GenerateNewCar()
        End If
        TimerDragDrop.Start()
    End Sub
    Private Sub TSElements_MouseUp(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBMilestone.MouseUp, TSBPhase.MouseUp, TSBGroup.MouseUp, TSBCar.MouseUp
        TimerDragDrop.Stop()
    End Sub
    Private Sub TSElements_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBMilestone.Click, TSBPhase.Click, TSBGroup.Click, TSBCar.Click
        Dim ObjectToAdd As New Object
        If sender Is TSBMilestone Then
            ObjectToAdd = GenerateNewMilestone()
        ElseIf sender Is TSBPhase Then
            ObjectToAdd = GenerateNewProjectElement()
        ElseIf sender Is TSBGroup Then
            ObjectToAdd = GenerateNewGroup()
        ElseIf sender Is TSBCar Then
            ObjectToAdd = GenerateNewCar()
        End If
        ObjectAdditionRequest(ObjectToAdd)
    End Sub
    Private Sub TimerDragDrop_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerDragDrop.Tick
        TimerDragDrop.Stop()
        Me.DoDragDrop(DragObject, Windows.Forms.DragDropEffects.Copy)
    End Sub
    Private Sub cmdUndo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBUndo.Click
        ActiveProject.Undo()
    End Sub
    Private Sub cmdRedo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBRedo.Click
        ActiveProject.Redo()
    End Sub
#End Region
#Region "TRASH MANAGEMENT"
    Private Sub cmdTrash_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles cmdTrash.DragDrop
        If e.Data.GetDataPresent(GetType(DragDropStructure)) = True Then
            Dim Source As DragDropStructure = e.Data.GetData(GetType(DragDropStructure))
            With Source
                Select Case .LowestLevelObject
                    Case DragDropStructure.LowestLevelValues.Car
                        ActiveProject.MakeUndoPoint(UndoType.CarRemove)
                        .Group.Cars.RemoveAt(.Group.Cars.IndexOf(.Car))
                    Case DragDropStructure.LowestLevelValues.Group
                        ActiveProject.MakeUndoPoint(UndoType.GroupRemove)
                        .Element.Groups.RemoveAt(.Element.Groups.IndexOf(.Group))
                    Case DragDropStructure.LowestLevelValues.Element
                        ActiveProject.MakeUndoPoint(UndoType.ElRemove)
                        .Project.ProjectElements.RemoveAt(.Project.ProjectElements.IndexOf(.Element))
                End Select
            End With
        End If
    End Sub
    Private Sub cmdTrash_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles cmdTrash.DragEnter
        e.Effect = Windows.Forms.DragDropEffects.All
    End Sub
#End Region
End Class
